package edu.yngsxy.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.yngsxy.demo.mapper.TProductMapper;
import edu.yngsxy.demo.model.TProduct;
import edu.yngsxy.demo.model.TUser;
import edu.yngsxy.demo.service.ProductService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private TProductMapper tProductMapper;

    /**
     * 展示所有商品
     *
     * @return
     */
    @Override
    public List<TProduct> getProductList(String productname,String brand) {
        QueryWrapper<TProduct> wrapper = new QueryWrapper<>();
        if(productname==null){
            productname="";
        }
        if(brand==null){
            brand = "";
        }
        wrapper.like("productname",productname);
        wrapper.like("brand",brand);
        return tProductMapper.selectList(wrapper);
    }

    /**
     * 查看详细商品说明
     *
     * @param productID
     * @return
     */
    @Override
    public TProduct getProductDetail(int productID) {

        return tProductMapper.selectById(productID);
    }

    /**
     * 添加商品
     *
     * @param tProduct
     * @return
     */
    @Override
    public boolean addProduct(TProduct tProduct) {
        int a = tProductMapper.insert(tProduct);
        return a > 0;
    }

    /**
     * 修改商品信息
     *
     * @param tProduct
     * @return
     */
    @Override
    public boolean modProduct(@Validated TProduct tProduct) {
        QueryWrapper<TProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("productid",tProduct.getProductid());
        int a = tProductMapper.update(tProduct,wrapper);
        //int a = tProductMapper.updateById(tProduct);
        return a>0;
    }

    /**
     * 删除商品信息
     *
     * @param productID
     * @return
     */
    @Override
    public boolean deleteProduct(int productID) {
        int a = tProductMapper.deleteById(productID);
        return a>0;
    }

    /**
     * 根据userid查询这个人下面的所有商品
     *
     * @param userid
     * @return
     */
    @Override
    public List<TProduct> productAll(Integer userid) {
        QueryWrapper<TProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("sellerid",userid);
        return tProductMapper.selectList(wrapper);
    }
}
